package com.mysql;

import java.sql.*;
import java.util.Scanner;

public class GetMysqlMian {
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url = "jdbc:mysql://xxx:3309/xxxx";
                String user = "user";
                String pass = "pass";
                conn = DriverManager.getConnection(url, user, pass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
        public static void main(String[] args) {
            Connection conn = getConnection();
            String one="1";
            while (one.equals("1")){
                Scanner sc=new Scanner(System.in);
                System.out.print("数据数据表：");
                String shur=sc.nextLine();
                String sql = "select * from "+ shur;
                PreparedStatement stmt;
                try {
                    stmt = conn.prepareStatement(sql);
                    ResultSet rs = stmt.executeQuery(sql);
                    ResultSetMetaData data = rs.getMetaData();
                    String table="";

                    for (int i = 1; i <= data.getColumnCount(); i++) {
                        // 获得所有列的数目及实际列数
                        int columnCount = data.getColumnCount();
                        // 获得指定列的列名
                        String columnName = data.getColumnName(i);
                        // 获得指定列的列值
                        int columnType = data.getColumnType(i);
                        // 获得指定列的数据类型名
                        String columnTypeName = data.getColumnTypeName(i);
                        // 所在的Catalog名字
                        String catalogName = data.getCatalogName(i);
                        // 对应数据类型的类
                        String columnClassName = data.getColumnClassName(i);
                        // 在数据库中类型的最大字符个数
                        int columnDisplaySize = data.getColumnDisplaySize(i);
                        // 默认的列的标题
                        String columnLabel = data.getColumnLabel(i);
                        // 获得列的模式
                        String schemaName = data.getSchemaName(i);
                        // 某列类型的精确度(类型的长度)
                        int precision = data.getPrecision(i);
                        // 小数点后的位数
                        int scale = data.getScale(i);
                        // 获取某列对应的表名
                        String tableName = data.getTableName(i);
                        // 是否自动递增
                        boolean isAutoInctement = data.isAutoIncrement(i);
                        // 在数据库中是否为货币型
                        boolean isCurrency = data.isCurrency(i);
                        // 是否为空
                        int isNullable = data.isNullable(i);
                        // 是否为只读
                        boolean isReadOnly = data.isReadOnly(i);
                        // 能否出现在where中
                        boolean isSearchable = data.isSearchable(i);
//                    System.out.println("获得列" + i + "的类型,返回SqlType中的编号:" + columnType);
                        String s=columnTypeName;
                        char [] a=s.toCharArray();//把字符序列存放在数组里面
                        for(int x=0;x<a.length;x++){
                            if(a[x]>='A'&& a[x]<='Z'){
                                a[x]=(char)(a[x]+32);
                            }
                            else if(a[x]>='a'&& a[x]<='z'){
                                a[x]=(char)(a[x]-32);
                            }
                        }
                        String typename="";
                        for(int x=0;x<a.length;x++) {
                            typename += a[x];
                        }
                        table+="<tr>\n";
                        table+="\t<td>"+i+"</td>\n";
                        if (i==1){
                            table+="\t<td><span>#</span><span>*</span>"+columnLabel+"</td>\n";
                        }else{
                            table+="\t<td>"+columnLabel+"</td>\n";
                        }
                        if (typename.equals("varchar")){
                            table+="\t<td>"+typename+"("+columnDisplaySize+")"+"</td>\n";
                        }else if (typename.equals("double")){
                            table+="\t<td>"+typename+"("+columnDisplaySize+","+scale+")"+"</td>\n";
                        }else {
                            table+="\t<td>"+typename+"</td>\n";
                        }
                        table+="\t<td>注释</td>\n";
                        table+="</tr>\n";
                    }
                    System.out.println(table);
                    Scanner trueA=new Scanner(System.in);
                    System.out.print("是否继续输入（1位继续，其他为退出）：");
                    one=trueA.nextLine();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

}
